package com.huawei.petapp2.service;

import android.content.Context;
import android.text.TextUtils;

import com.huawei.petapp2.utils.LoginUtil;
import com.huawei.petapp2.utils.SPUtil;

/**
 * 文 件 名: MemberRight.java
 * 版    权: Copyright Huawei Tech.Co.Ltd. All Rights Reserved.
 * 描    述: 会员权限管理
 */
public class MemberRightService {
    public static final String NORMAL_MEMBER = "normal";
    public static final long ONE_DAY = 24 * 60 * 60 * 1000;
    private static final String IMAGE_NORMAL_KEY = "imageNormalKey";
    private static final String UNKNOWN = "unknown";

    public static String getCurrentUserId(Context context) {
        String localUserUuid = LoginUtil.getLocalUserUuid(context);
        if (!TextUtils.isEmpty(localUserUuid)) {
            return localUserUuid;
        } else {
            return UNKNOWN;
        }
    }

    /**
     * 获取会员类型
     *
     * @param context context
     * @return string
     */
    public static String getMemberType(Context context) {
        if (System.currentTimeMillis() < getNormalImageExpireDate(context)) {
            return NORMAL_MEMBER;
        }
        return "";
    }

    /**
     * 是否有权限观看图片
     *
     * @param context 上下文
     * @return boolean
     */
    public static boolean isImageAvailable(Context context) {
        return System.currentTimeMillis() < getNormalImageExpireDate(context);
    }

    /**
     * 普通会员有效期时间
     *
     * @param context 上下文
     * @return 时间戳
     */
    public static long getNormalImageExpireDate(Context context) {
        return (long) SPUtil.get(context, getCurrentUserId(context), IMAGE_NORMAL_KEY, 0L);
    }

    /**
     * 更新普通会员有效期
     *
     * @param context   上下文
     * @param extension 有效时间段
     */
    public static void updateNormalVideoValidDate(Context context, long extension) {
        long imageExpireDate = getNormalImageExpireDate(context);
        long currentTime = System.currentTimeMillis();
        if (currentTime < imageExpireDate) {
            imageExpireDate += extension;
        } else {
            imageExpireDate = currentTime + extension;
        }
        SPUtil.put(context, getCurrentUserId(context), IMAGE_NORMAL_KEY, imageExpireDate);
    }
}